package com.example.controller;

import com.example.dto.RiskIndicatorDTO;
import com.example.service.RiskCalculationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 风控指标计算控制器
 * 提供风控指标计算的API接口
 */
@Slf4j
@RestController
@RequestMapping("/api/risk-calculation")
@RequiredArgsConstructor
public class RiskCalculationController {

    private final RiskCalculationService riskCalculationService;

    /**
     * 计算交易风险指标
     */
    @PostMapping("/transaction/{userId}")
    public ResponseEntity<RiskIndicatorDTO> calculateTransactionRisk(@PathVariable String userId) {
        log.info("API - Calculating transaction risk for user: {}", userId);
        RiskIndicatorDTO indicator = riskCalculationService.calculateTransactionRiskIndicator(userId);
        return ResponseEntity.ok(indicator);
    }

    /**
     * 计算登录风险指标
     */
    @PostMapping("/login/{userId}")
    public ResponseEntity<RiskIndicatorDTO> calculateLoginRisk(@PathVariable String userId) {
        log.info("API - Calculating login risk for user: {}", userId);
        RiskIndicatorDTO indicator = riskCalculationService.calculateLoginRiskIndicator(userId);
        return ResponseEntity.ok(indicator);
    }

    /**
     * 计算账户风险指标
     */
    @PostMapping("/account/{userId}")
    public ResponseEntity<RiskIndicatorDTO> calculateAccountRisk(@PathVariable String userId) {
        log.info("API - Calculating account risk for user: {}", userId);
        RiskIndicatorDTO indicator = riskCalculationService.calculateAccountRiskIndicator(userId);
        return ResponseEntity.ok(indicator);
    }

    /**
     * 计算综合风险指标
     */
    @PostMapping("/overall/{userId}")
    public ResponseEntity<RiskIndicatorDTO> calculateOverallRisk(@PathVariable String userId) {
        log.info("API - Calculating overall risk for user: {}", userId);
        RiskIndicatorDTO indicator = riskCalculationService.calculateOverallRiskIndicator(userId);
        return ResponseEntity.ok(indicator);
    }

    /**
     * 计算所有风险指标
     */
    @PostMapping("/all/{userId}")
    public ResponseEntity<Map<String, RiskIndicatorDTO>> calculateAllRisks(@PathVariable String userId) {
        log.info("API - Calculating all risk indicators for user: {}", userId);
        
        // 计算各类风险指标
        RiskIndicatorDTO transactionRisk = riskCalculationService.calculateTransactionRiskIndicator(userId);
        RiskIndicatorDTO loginRisk = riskCalculationService.calculateLoginRiskIndicator(userId);
        RiskIndicatorDTO accountRisk = riskCalculationService.calculateAccountRiskIndicator(userId);
        RiskIndicatorDTO overallRisk = riskCalculationService.calculateOverallRiskIndicator(userId);
        
        // 组装结果
        Map<String, RiskIndicatorDTO> result = new HashMap<>();
        result.put("transactionRisk", transactionRisk);
        result.put("loginRisk", loginRisk);
        result.put("accountRisk", accountRisk);
        result.put("overallRisk", overallRisk);
        
        return ResponseEntity.ok(result);
    }

    /**
     * 批量计算用户的综合风险指标
     */
    @PostMapping("/overall/batch")
    public ResponseEntity<List<RiskIndicatorDTO>> calculateOverallRiskBatch(@RequestBody List<String> userIds) {
        log.info("API - Calculating overall risk for {} users", userIds.size());
        
        // 为每个用户计算综合风险指标
        List<RiskIndicatorDTO> indicators = userIds.stream()
                .map(riskCalculationService::calculateOverallRiskIndicator)
                .toList();
        
        return ResponseEntity.ok(indicators);
    }
}